package com.qiwo.destop.product;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.qiwo.webshow.domain.ProductDO;
import com.qiwo.webshow.domain.ProductImgDO;
import com.qiwo.webshow.domain.SeriesintroDO;
import com.qiwo.webshow.domain.SerisesDO;
import com.qiwo.webshow.service.ProductImgService;
import com.qiwo.webshow.service.ProductService;
import com.qiwo.webshow.service.SeriesintroService;
import com.qiwo.webshow.service.SerisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/26 0026
 * Description:
 */
@Service
public class GetProduct {

    @Autowired
    private ProductService productService;
    /**
     * 获取产品list
     * @param seriesId
     * @return
     */
    public   List<ProductDO> getProduct(Integer seriesId){

        Wrapper<ProductDO> productDOWrapper=new EntityWrapper<>();
        productDOWrapper.eq("series_introid",seriesId).orderBy("sort",true);
        List<ProductDO> productDOList=productService.selectList(productDOWrapper);
        return  productDOList;
    }

    public ProductDO getProductDetail(Integer productId){
        ProductDO productDO=productService.selectById(productId);
        return  productDO;
    }
   @Autowired
   private ProductImgService productImgService;
    public  List<ProductImgDO> getProductImg(Integer productId){
        Wrapper<ProductImgDO> imgDOWrapper=new EntityWrapper<>();
        imgDOWrapper.eq("product_id",productId).orderBy("sort",true);
        List<ProductImgDO> imgDOS=productImgService.selectList(imgDOWrapper);
        return  imgDOS;
    }
    @Autowired
    private SerisesService serisesService;
    @Autowired
    private SeriesintroService seriesintroService;
    public SeriesintroDO getSeriesInfo(Integer seriesId){
        SeriesintroDO  seriesintroDO= seriesintroService.selectById(seriesId);
        List<SerisesDO> serisesDOList = serisesService.selectList(null);
        if (seriesintroDO.getSeriesId() != null) {
            Optional<SerisesDO> optionalSerisesDO = serisesDOList.stream().filter(f -> {
                return f.getId().equals(seriesintroDO.getSeriesId());
            }).findFirst();
            if (optionalSerisesDO.isPresent()) {
                seriesintroDO.setSeriesName(optionalSerisesDO.get().getName());
            }
        }
        return  seriesintroDO;
    }
}
